// 路由权限控制，路由守卫
import router from './router'
// 引入仓库
import store from './store'

const whiteList = ['/login', '/404']

router.beforeEach((to, from, next) => {
  const token = store.state.user.token
  if (!token) {
    // 如果没有token，只能去登录页和404页面
    const res = whiteList.includes(to.path)
    if (res) {
      next()
    } else {
      next({ path: '/login' })
    }
  } else {
    // 如果有token，除了登录页以外哪儿都能去
    if (to.path === '/login') {
      next({ path: '/' })
    } else {
      next()
    }
  }
})
